package com.dhl.cloud.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.dhl.cloud.course.service.CourseService;
import com.dhl.cloud.course.service.LessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dhl.cloud.common.Constants;
import com.dhl.cloud.common.http.HttpResult;
import com.dhl.cloud.common.vo.ComFrontBean;
import com.dhl.cloud.course.entity.LessionTransfile;
import com.dhl.cloud.course.service.LessionTransfileService;

@RestController
@RequestMapping(value = "/user/")
public class LessionTransfileController extends BaseController{

	@Autowired
	private LessionTransfileService lessionTransfileService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private LessionService lessionService;

    @GetMapping("getMyLessionTransfileList")
    public HttpResult getMyLessionTransfileList(Integer lessionid)
    {
        List<LessionTransfile> list = lessionTransfileService.selectByLessionid(lessionid);
        //简单处理下，返回：course名字+lesson名字+第几条录音
        for (int i = 0;i<list.size();i++) {
            LessionTransfile lt = list.get(i);
            String name = courseService.selectByPrimaryKey(lt.getCourseid()).getName()+" - "
                    +lessionService.selectByPrimaryKey(lt.getLessionid()).getName()+" - "
                    + (i+1);
            lt.setName(name);
        }
        return HttpResult.success("",list);
    }

    @PostMapping("deleteLessionTransfile")
    public HttpResult deleteLessionTransfile(@RequestBody ComFrontBean ComFrontBean,HttpServletRequest request)
    {
        int flag = lessionTransfileService.deleteByPrimaryKey(ComFrontBean.getId());
        return flag==1 ? HttpResult.success("删除成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }

    @PostMapping("saveLessionTransfile")
    public HttpResult saveLessionTransfile(@RequestBody LessionTransfile lessionfile,HttpServletRequest request)
    {
        int flag = lessionTransfileService.insert(lessionfile);
        return flag==1 ? HttpResult.success("操作成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }
}
